package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.OrderDetailService;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户往购物车添加商品
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        //得到shoppingCart没有userId  手动赋值
        Long userId = (Long) session.getAttribute("userId");
        log.info("添加到用户'{}'购物车:{}",userId, shoppingCart);
        //查询数据库中有没有用户添加的同种菜品或者套餐
        //设置查询条件  菜品id或者套餐id  number!=null
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId())
                .eq(userId!=null,ShoppingCart::getUserId,userId);
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if(one!=null){
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
        shoppingCart.setUserId(userId);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartService.save(shoppingCart);
        return R.success(shoppingCart);
    }

    /**
     * 用户查询购物车内容
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R list(HttpSession session){
        //获得用户id
        Long userId = (Long) session.getAttribute("userId");
        log.info("根据用户id查询购物车:{}",userId);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 用户减少购物车商品
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        Long userId = (Long) session.getAttribute("userId");
        log.info("用户'{}'购物车减物品:{}",userId,shoppingCart);
        //设置修改条件
        //判断商品数量是否为1   如果为1  删除商品   如果大于1  商品数量-1
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId())
                .eq(userId!=null,ShoppingCart::getUserId,userId);
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if(one.getNumber()==null){
            return R.error("失败");
        }
        if (one.getNumber()>1) {
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
            return R.success("成功");
        }
        if(one.getNumber()==1){
            shoppingCartService.removeById(one);
            return R.success("成功");
        }
        return R.error("失败");
    }

    /**
     * 用户清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R clean(HttpSession session){
        Long userId = (Long) session.getAttribute("userId");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        shoppingCartService.remove(wrapper);
        return R.success("成功");
    }
}
